package com.fcmeditech.AsianSexyGirls;

import java.util.ArrayList;

import com.fcmeditech.AsianSexyGirls.File.BitmapManager;
import com.fcmeditech.AsianSexyGirls.File.BitmapManager2;
import com.fcmeditech.AsianSexyGirls.File.ImageDownloader;
import com.fcmeditech.AsianSexyGirls.File.ImageLoaderDefault;



import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;

public class Galleryadapter extends BaseAdapter{
	 private Context context;
	 public ImageLoaderDefault imageLoader; 
	 public ImageDownloader load=new ImageDownloader(); 
	 private ArrayList<ImageObject> listimg;
	 //BitmapManager bitmapManager;
	 private BitmapManager2 bitmapManager2;
	
	
	public Galleryadapter(Context c, ArrayList<ImageObject> listimg) {
		super();
		this.context=c;
		this.listimg = listimg;
		imageLoader=new ImageLoaderDefault(c);
		//BitmapManager.INSTANCE.setPlaceholder(BitmapFactory.decodeResource(context.getResources(), R.drawable.xinhdep1));
		bitmapManager2=new BitmapManager2(1);
		//bitmapManager2.setPlaceholder(BitmapFactory.decodeResource(context.getResources(), R.drawable.xinhdep1));
		
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return listimg.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			View vi=convertView;
			ImageView image=null;
	        ImageObject myImg= listimg.get(position);
	        GalleryHolder holder=null;
	        
	        if(convertView==null){
	        	LayoutInflater inflater = ((Activity)context).getLayoutInflater();
	            vi = inflater.inflate(R.layout.displayimage, parent, false);  //insert layout of ROW
	            holder =new GalleryHolder();
	            
	            holder.imageView=(ImageView)vi.findViewById(R.id.imageDisplay);
	            holder.progressBar=(ProgressBar)vi.findViewById(R.id.progressBar1);
	            //RelativeLayout layout = (RelativeLayout) vi.findViewById(R.id.ProgressBarLayou);
	            vi.setTag(holder);
	        }
	        else holder=(GalleryHolder)vi.getTag();
	        //------------------set click on ImageView-----------------------------
	        
	        ///------------- link ----------------------
	        String link=myImg.getImgLink();
	        
	        //**************************Method 1: no other thread, simple decode **********************************
	        //Bitmap result=load.simpleDownloadBitmap(link); // There are sometimes lossing images!
	        //image.setImageBitmap(result);
	       
	        //******************************Method 2:*********************************************
	        //calculate time to load:
	       /*
	        Bitmap result=load.fineDownloadBitmap(link); // There are sometimes lossing images!
	        
	        listimg.get(position).setmImage(result);
	        
	        //change dimension angle if height< width;
	        int height=result.getHeight();
	        int width=result.getWidth();
	        if(height<width){
	        	Matrix mat=new Matrix();
	        	mat.preRotate(90);
	        	Bitmap newBmp=Bitmap.createBitmap(result, 0, 0, width, height, mat, true);
	        	image.setImageBitmap(newBmp);
	        	
	        }else  image.setImageBitmap(result);*/
	        
	        
	        //********************************Method 3: async thread, load images in the order, *************************
	        // not save images so when scroll LISTVIEW, we again download and display in Avatar !!!
	        /* long loadtime=System.currentTimeMillis();
	         load.download(link, image);
	         */
	        //Bitmap temp=null;
	        //load.download(link, image,temp);
	        // Bitmap temp=load.getCurrentImage();
	        //listimg.get(position).setmImage(temp);
	         //myImg.setmImage(temp); --> only return null??
	        
	        //---> error
	         //*******************************Method 4:**************************************************
	        //imageLoader.DisplayImage(link, image);
	        //********************************Method 5:********************************************
	        //IMAGEs downloaded from url are rotated if height<width
	        //BitmapManager.INSTANCE.loadBitmap(link, image, image.getWidth(),image.getHeight()); //--->load bitmap from Cache 
	        //Bitmap result=BitmapManager.INSTANCE.getBitmapFromCache(link);
	        bitmapManager2.loadBitmap(link, holder.imageView, holder.imageView.getWidth(),holder.imageView.getHeight(),position); //--->load bitmap from Cache 
	        Bitmap result=bitmapManager2.getBitmapFromCache(link,position);
	        
	        listimg.get(position).setmImage(result);
	        
	        
		return vi;	
	}
	static class GalleryHolder{
		ImageView imageView;
		ProgressBar progressBar;
		
	}
}
